package net.lijingye.education.controller;

import net.lijingye.education.entity.Friend;
import net.lijingye.education.entity.User;
import net.lijingye.education.exception.DaoException;
import net.lijingye.education.exception.FriendRequestAlreadyExistsException;
import net.lijingye.education.exception.ObjectNotExistsException;
import net.lijingye.education.response.BaseResponse;
import net.lijingye.education.response.ErrorResponse;
import net.lijingye.education.response.SuccessResponse;
import net.lijingye.education.response.meta.FriendRequestAlreadyExistsMeta;
import net.lijingye.education.response.meta.ObjectNotExistsMeta;
import net.lijingye.education.service.UserService;
import net.lijingye.education.util.CommonUtil;
import net.lijingye.education.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by LijingYe on 2016/3/27.
 */
@RestController
public class UserController extends BaseController {
    @Autowired
    private UserService userService;

    @RequestMapping(value = "/users", method = RequestMethod.GET)
    public BaseResponse getUsers(@RequestParam(value = "word", required = false) String word, @RequestParam(value = "page", required = false) String page) {
        List<UserProfileLiteVO> userProfileLiteVOList = new ArrayList<>();
        //当查询筛选器为空时,不进行查询
        if (word == null || word.trim().equals("")) {
            return new SuccessResponse(userProfileLiteVOList);
        }
        // Static result
        System.out.println(word);
        System.out.println(page);
        userProfileLiteVOList.add(new UserProfileLiteVO("10000000004", "测试4", null, "北京市-朝阳区", true));
        userProfileLiteVOList.add(new UserProfileLiteVO("10000000005", "测试5", null, "河南省-安阳市", false));
        return new SuccessResponse(userProfileLiteVOList, new PageVO(2, 12, 1, 1));
    }

    @RequestMapping(value = "/users/{userId}/profiles", method = RequestMethod.GET)
    public BaseResponse getUserProfiles(@RequestHeader("Authorization") String accessToken, @PathVariable String userId) throws DaoException {
        try {
            User current = userService.getUserByAccessToken(accessToken);
            if (current.getId().equals(userId)) {
                MyProfileVO myProfileVO = new MyProfileVO(current);
                return new SuccessResponse(myProfileVO);
            } else {
                User user = userService.getUserById(userId);
                UserProfileVO userProfileVO = new UserProfileVO(user, userService.isFriend(current.getId(), user.getId()));
                return new SuccessResponse(userProfileVO);
            }
        } catch (ObjectNotExistsException e) {
            System.out.println(e.getMessage());
            return new ErrorResponse(new ObjectNotExistsMeta());
        }
    }

    @RequestMapping(value = "/users/{userId}/friends", method = RequestMethod.GET)
    public BaseResponse getUserFriends(@PathVariable String userId, @RequestParam(value = "page", required = false) String page) throws DaoException {
        try {
            User user = userService.getUserById(userId);
            List<Friend> friendList = user.getFriendList();
            List<FriendVO> friendVOList = new ArrayList<>();
            friendList.forEach(friend -> {
                friendVOList.add(new FriendVO(friend));
            });
            return CommonUtil.splitPage(friendVOList, page, super.getPageSize());
        } catch (ObjectNotExistsException e) {
            System.out.println(e.getMessage());
            return new ErrorResponse(new ObjectNotExistsMeta());
        }
    }

    @RequestMapping(value = "/users/{userId}/requests", method = RequestMethod.POST)
    public BaseResponse postUserFriendRequest(@RequestHeader("Authorization") String accessToken, @PathVariable String userId) throws DaoException {
        try {
            User current = userService.getUserByAccessToken(accessToken);
            User user = userService.getUserById(userId);
            userService.addFriendRequest(current, user);
            return new SuccessResponse(null);
        } catch (ObjectNotExistsException e) {
            System.out.println(e.getMessage());
            return new ErrorResponse(new ObjectNotExistsMeta());
        } catch (FriendRequestAlreadyExistsException e) {
            System.out.println(e.getMessage());
            return new ErrorResponse(new FriendRequestAlreadyExistsMeta());
        }
    }
}
